import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class TouchEventTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TouchEventTestState();
  }
}

class _TouchEventTestState extends State<TouchEventTest> {
  PointerEvent _event;
  String _operation = "No Gesture detected!"; //保存事件名
  var _top = 0.0;
  var _left = 0.0;
  final GlobalKey globalKey = GlobalKey();
  final GlobalKey globalKeyRoot = GlobalKey();
  var _imagesWidth = 200.0;
  TapGestureRecognizer _recognizer = new TapGestureRecognizer();
  bool _isTap = false;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _recognizer.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text("触摸事件测试"),
        ),
        body: Center(
            child: Column(
          children: <Widget>[
            Listener(
              child: Container(
                width: 200,
                height: 100,
                padding: EdgeInsets.all(15),
                alignment: Alignment.center,
                color: Colors.greenAccent,
                child: Text(
                  _event?.toString() ?? "",
                  style: TextStyle(color: Colors.white, fontSize: 15),
                ),
              ),
              onPointerDown: (event) {
                setState(() {
                  _event = event;
                });
              },
              onPointerMove: (event) {
                setState(() {
                  _event = event;
                });
              },
              onPointerUp: (event) {
                setState(() {
                  _event = event;
                });
              },
              onPointerSignal: (event) {
                setState(() {
                  _event = event;
                });
              },
            ),
            GestureDetector(
              child: Container(
                margin: EdgeInsets.only(top: 15),
                width: 200,
                height: 100,
                alignment: Alignment.center,
                color: Colors.greenAccent,
                child: Text(
                  _operation,
                  style: TextStyle(color: Colors.white, fontSize: 15),
                ),
              ),
              onTap: () {
                updateText(text: "onTap");
              },
              onLongPress: () {
                updateText(text: "onLongPress");
              },
              onLongPressEnd: (details) {
                updateText(text: "onLongPressEnd:$details");
              },
            ),
            Container(
              key: globalKeyRoot,
              margin: EdgeInsets.only(top: 15),
              width: 200,
              height: 100,
              alignment: Alignment.center,
              color: Colors.greenAccent,
              child: Stack(
                children: <Widget>[
                  Positioned(
                    child: GestureDetector(
                      child: CircleAvatar(
                        key: globalKey,
                        backgroundColor: Colors.deepOrange,
                        child: Text("A",
                            style:
                                TextStyle(color: Colors.white, fontSize: 25)),
                      ),
                      onPanDown: (e) {
                        log(e.toString(), name: "zhangchen");
                      },
                      onPanUpdate: (e) {
                        BuildContext c = globalKey.currentContext;
                        var width = c.size.width;
                        var height = c.size.height;

                        BuildContext c2 = globalKeyRoot.currentContext;
                        var widthRoot = c2.size.width;
                        var heightRoot = c2.size.height;

                        print(heightRoot);

                        var tempLeft = _left;
                        var tempTop = _top;
                        tempLeft += e.delta.dx;
                        tempTop += e.delta.dy;
                        if (tempLeft >= 0 && tempLeft <= (widthRoot - width)) {
                          _left = tempLeft;
                        }
                        if (tempTop >= 0 &&
                            tempTop <= (heightRoot - height - 15)) {
                          _top += e.delta.dy;
                        }
                        setState(() {});
                      },
                      onPanEnd: (e) {
                        log(e.velocity.toString(), name: "zhangchen");
                      },
                    ),
                    left: _left,
                    top: _top,
                  ),
                ],
              ),
            ),
            Container(
                margin: EdgeInsets.only(top: 15),
                child: GestureDetector(
                  child: Image.asset(
                    "bizhi.jpg",
                    width: _imagesWidth,
                  ),
                  onScaleUpdate: (e) {
                    setState(() {
                      _imagesWidth = 200 * e.scale.clamp(0.5, 5);
                    });
                  },
                )),
            Padding(
                padding: EdgeInsets.only(top: 15),
                child: Text.rich(TextSpan(children: <TextSpan>[
                  TextSpan(text: 'hello word'),
                  TextSpan(
                      text: "点击变色",
                      recognizer: _recognizer
                        ..onTap = () {
                          setState(() {
                            _isTap = !_isTap;
                          });
                        },
                      style: TextStyle(
                          color: _isTap ? Colors.deepOrange : Colors.green)),
                  TextSpan(text: '你好事件')
                ])))
          ],
        )));
  }

  void updateText({@required String text}) {
    setState(() {
      _operation = text;
    });
  }
}
